package com.ccys.cms.service.userManagement.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.userManagement.DoctorMessageTemplateService;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.userManagement.DoctorMessageTemplateDto;
import com.ccys.db.entity.TblDoctorMessageTemplate;
import com.ccys.db.mapper.TblDoctorMessageTemplateMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.userManagement.DoctorMessageTemplatePageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 医生快捷回复接口实现类
 * @author: ZhangJingCan
 * @create: 2023-12-20 14:01
 **/

@Service
public class DoctorMessageTemplateServiceImpl extends BaseServiceImpl<TblDoctorMessageTemplate> implements DoctorMessageTemplateService {

    @Resource
    private TblDoctorMessageTemplateMapper tblDoctorMessageTemplateMapper;

    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveDoctorMessageTemplate(DoctorMessageTemplateDto dto) {
        //校验字段
        Kit.allParamValid(dto, "name", "groupId", "isDefault", "message", "dispOrder");
        //准备数据用于查询是否存在重复
        Example example = new Example(TblDoctorMessageTemplate.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("groupId", dto.getGroupId()).andEqualTo("name", dto.getName());
        //用于返回错误信息的字符串
        String error = "This name already exists in the current group!";
        //校验是医生操作还是后台操作 - 医生操作需要传入医生ID
        if (0 == dto.getIsDefault()) {
            if (ObjectUtil.isEmpty(dto.getDoctorId())) {
                return ResponseResult.error("Parameter abnormality!");
            }
            //如果属于医生 - 需要校验这个医生是否存在当前name
            criteria.andEqualTo("doctorId", dto.getDoctorId());
            //如果存在医生id - 返回的错误信息带上医生
            error = "The current group's doctor already has this name!";
        }
        if (ObjectUtil.isNotEmpty(dto.getId())) {
            //如果ID存在 - 属于修改 - 需要排除自身
            criteria.andNotEqualTo("id", dto.getId());
        }
        //查询是否存在
        if (ObjectUtil.isNotEmpty(tblDoctorMessageTemplateMapper.selectOneByExample(example))) {
            //存在则返回错误信息
            return ResponseResult.error(error);
        }
        //以上都没问题则开始新增或修改
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteDoctorMessageTemplate(DoctorMessageTemplateDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblDoctorMessageTemplate template = new TblDoctorMessageTemplate();
        template.setId(dto.getId());
        template.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblDoctorMessageTemplateMapper.updateByPrimaryKeySelective(template) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateDoctorMessageTemplateStatus(DoctorMessageTemplateDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据修改
        TblDoctorMessageTemplate template = new TblDoctorMessageTemplate();
        template.setId(dto.getId());
        template.setStatus(dto.getStatus());
        if (tblDoctorMessageTemplateMapper.updateByPrimaryKeySelective(template) > 0) {
            //保存状态修改日志
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateDoctorMessageTemplateDispOrder(DoctorMessageTemplateDto dto) {
        //校验字段
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getDispOrder())) {
            return ResponseResult.error("illegal request!");
        }
        TblDoctorMessageTemplate template = new TblDoctorMessageTemplate();
        template.setId(dto.getId());
        template.setDispOrder(dto.getDispOrder());
        if (tblDoctorMessageTemplateMapper.updateByPrimaryKeySelective(template) > 0) {
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    public ResponseResult<PageInfo<DoctorMessageTemplatePageQueryVo>, Object> findPageDoctorMessageTemplate(DoctorMessageTemplateDto dto) {
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        List<DoctorMessageTemplatePageQueryVo> list = tblDoctorMessageTemplateMapper.findPageDoctorMessageTemplate(dto);
        PageInfo<DoctorMessageTemplatePageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }
}
